package com.sandking.db.jedis;

import java.util.ArrayList;
import com.sandking.db.dao.LmsjDao;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import com.sandking.db.bean.Lmsj;
import java.util.Set;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class LmsjJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Lmsj getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Lmsj lmsj = null;
		try{
			String key = "Lmsj_Object";
			String field = SK_Plus.b("id:",id).e();
			lmsj = Lmsj.createForJson(jedis.hget(key,field));
			if(lmsj == null){
				lmsj = LmsjDao.getById(id);
				if(lmsj!=null){
					loadCache(lmsj);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return lmsj;
	}
	
	/**
	 * 根据( 用户_id ) 查询
	 */
	public static List<Lmsj> getByYh_id(int yh_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Lmsj> lmsjs = new ArrayList<Lmsj>();
		try{
			String key = "Lmsj_Index";
			String field = SK_Plus.b(key,"yh_id:",yh_id).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Lmsj_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				lmsjs = Lmsj.createForJson(jsons);
			}else{
				lmsjs = LmsjDao.getByYh_id(yh_id);
				if(lmsjs!=null && !lmsjs.isEmpty()){
					loadCaches(lmsjs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return lmsjs;
	}
	
	/**
	 * 根据( 联盟_id ) 查询
	 */
	public static List<Lmsj> getByLm_id(int lm_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Lmsj> lmsjs = new ArrayList<Lmsj>();
		try{
			String key = "Lmsj_Index";
			String field = SK_Plus.b(key,"lm_id:",lm_id).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Lmsj_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				lmsjs = Lmsj.createForJson(jsons);
			}else{
				lmsjs = LmsjDao.getByLm_id(lm_id);
				if(lmsjs!=null && !lmsjs.isEmpty()){
					loadCaches(lmsjs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return lmsjs;
	}
	
	/**
	 * 根据( 事件类型  联盟_id ) 查询
	 */
	public static List<Lmsj> getBySjlxLm_id(int sjlx, int lm_id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		List<Lmsj> lmsjs = new ArrayList<Lmsj>();
		try{
			String key = "Lmsj_Index";
			String field = SK_Plus.b(key,"sjlx, lm_id:",sjlx, lm_id).e();
			Set<String> setStr = jedis.smembers(field);
			if(setStr!=null){
				key = "Lmsj_Object";
				String[] fieldArray = (String[]) setStr.toArray();
				List<String> jsons = jedis.hmget(key,fieldArray);
				lmsjs = Lmsj.createForJson(jsons);
			}else{
				lmsjs = LmsjDao.getBySjlxLm_id(sjlx, lm_id);
				if(lmsjs!=null && !lmsjs.isEmpty()){
					loadCaches(lmsjs);
				}
			}
			
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		return lmsjs;
	}
	

	public static void loadCache(Lmsj lmsj){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(lmsj,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Lmsj lmsj,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Lmsj_Object";
		String field = SK_Plus.b("id:",lmsj.getId()).e();
		String data = lmsj.toJson();
		p.hset(key,field,data);
		
		String primaryKey = String.valueOf(lmsj.getId());
		key = "Lmsj_Index";
		// ---------------------- 聚集索引 ----------------------
		field = SK_Plus.b(key,"yh_id:",lmsj.getYh_id()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"lm_id:",lmsj.getLm_id()).e();
		p.sadd(field, primaryKey);
		
		field = SK_Plus.b(key,"sjlx, lm_id:",lmsj.getSjlx(),lmsj.getLm_id()).e();
		p.sadd(field, primaryKey);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Lmsj> lmsjs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Lmsj lmsj : lmsjs){
				loadCache(lmsj,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Lmsj lmsj){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(lmsj,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Lmsj lmsj,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Lmsj_Object";
		String field = SK_Plus.b("id:",lmsj.getId()).e();
		p.hdel(key,field);
		
		key = "Lmsj_Index";
		// ---------------------- 聚集索引 ----------------------
		String primaryKey = String.valueOf(lmsj.getId());
		field = SK_Plus.b(key,"yh_id:",lmsj.getYh_id()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"lm_id:",lmsj.getLm_id()).e();
		p.srem(field, primaryKey);
		
		field = SK_Plus.b(key,"sjlx, lm_id:",lmsj.getSjlx(),lmsj.getLm_id()).e();
		p.srem(field, primaryKey);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Lmsj> lmsjs){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Lmsj lmsj : lmsjs){
				clearCache(lmsj,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Lmsj insert(Lmsj lmsj){
		lmsj = LmsjDao.insert(lmsj);
		if(lmsj!=null){
			loadCache(lmsj);
		}
    	return lmsj;
    }
    
    public static Lmsj update(Lmsj lmsj){
    	lmsj = LmsjDao.update(lmsj);
    	if(lmsj!=null){
			loadCache(lmsj);
		}
    	return lmsj;
    }
    
    public static boolean delete(Lmsj lmsj){
    	boolean bool = LmsjDao.delete(lmsj);
    	if(bool){
    		clearCache(lmsj);
    	}
    	return bool;
    }
}